<template>
  <!-- <s-layout> -->
  <s-layout navbar="goods">
    <detailNavbar />
    <view class="list-item ss-p-b-20" v-if="classDetail">
      <view class="img-box">
        <image :src="sheep.$url.cdn(classDetail.logo)" mode="aspectFill" />
        <view :class="['status', getStatusInfo(classDetail.status).class]">{{ getStatusInfo(classDetail.status).text }}
        </view>
        <view class="tag" v-if="classDetail.courseTypeName">{{classDetail.courseTypeName}}</view>
      </view>
      <view class="item-card ss-p-y-20 ss-p-x-20">
        <view class="time-tab ss-flex">
          <view class="txt ss-flex-col ss-col-top">
            <view>本周</view>
            <view>课表</view>
          </view>
          <view class="tab-box ss-flex ss-row-between ss-flex-1" v-if="weekList.length">
            <view class="group ss-flex ss-row-center ss-flex-col ss-flex-1" v-for="(i,index) in weekList" :key="index">
              <text class="date">{{i.date}}</text>
              <text class="date rad" :class="{'act': classDetail?.dayList?.includes(i.i)}">{{i.day}}</text>
            </view>
          </view>
        </view>
      </view>
      <view class="item-card ss-p-y-20 ss-p-x-20">
        <view class="ss-flex ss-row-between">
          <view class="ss-flex">
            <view class="header-avatar">
              <image class="avatar-img"
                :src=" classDetail.studentAvatar ? sheep.$url.cdn(classDetail.studentAvatar) : 'https://txtdev-1252097407.cos.ap-guangzhou.myqcloud.com/static/img/shop/default_avatar.png'"
                mode="aspectFill" />
            </view>
            <text class="title-text ss-m-l-20">{{classDetail.studentName}}</text>
          </view>
          <view class="title-text">剩余课时：<text style="color:#FF6000">{{classDetail.rest||0}}节</text></view>
        </view>
        <view class="title-text ss-p-y-10">{{classDetail.name}}</view>

      </view>

      <view class="item-card ss-p-y-20 ss-p-x-20">
        <view class="title-card" @tap.stop="showMaoLocation(classDetail.storeInfo
)">
          <view class="ss-p-b-6 ss-flex ss-row-between" style="padding: 10rpx 0;">
            <view class="title-text">班级地点：</view>
            <view class="subtitle-text _icon-forward-after right-forwrad-icon">{{classDetail.storeName}}</view>
          </view>
          <view class="ss-p-b-6 ss-flex ss-row-between" style="padding: 10rpx 0;">
            <view class="title-text">小区主理人：</view>
            <view class="subtitle-text">{{classDetail.ownerName}}</view>
          </view>
          <view class="ss-p-b-6 ss-flex ss-row-between" style="padding: 10rpx 0;">
            <view class="title-text">运动陪伴师：</view>
            <view class="subtitle-text">{{classDetail.teacherName}}</view>
          </view>
          <view class="ss-p-b-6 ss-flex ss-row-between" style="padding: 10rpx 0;">
            <view class="title-text">开班日期：</view>
            <view class="subtitle-text">{{classDetail.startDate}}</view>
          </view>
          <view class="ss-p-b-6 ss-flex ss-row-between" style="padding: 10rpx 0;">
            <view class="title-text">结班日期：</view>
            <view class="subtitle-text">{{classDetail.endDate}}</view>
          </view>

        </view>
      </view>
      <view class="item-card">
        <view class="tab-list ss-flex ss-p-y-20 ss-p-x-20 ss-row-center" style="padding-bottom: 0;">
          <!-- <view class="tab-item" :class="{act: current == 0}" @click="selTab(0)">陪伴记录</view> -->
          <view class="tab-item" :class="{act: current == 1}" @click="selTab(1)">班级详情</view>
          <view class="tab-item" :class="{act: current == 2}" @click="selTab(2)">消课记录</view>
        </view>
        <!-- <view v-show="current == 0" class="tab-detail">
        </view> -->
        <view v-show="current == 1" class="tab-detail" style="padding: 0;">
          <mp-html v-if="classDetail.description" :content="classDetail.description" />
          <s-empty v-else text="暂无详情" />
        </view>
        <view v-show="current == 2" class="tab-detail use-history">
          <template v-if="classDetail.courseRecordList && classDetail.courseRecordList.length">
            <view class="use-list-item ss-flex ss-row-between" v-for="item in classDetail.courseRecordList"
              :key="item.id">
              <view class="time">{{item.dateTime}}</view>
              <view class="day">{{formartWeek(item.dateDay)}}</view>
              <view class="day">{{item.courseTimes || 1}}课时</view>
              <view :class="['sts', getXkStatusInfo(item.status).class]">{{getXkStatusInfo(item.status).text}}
              </view>
            </view>
          </template>
          <s-empty v-else text="暂无消课记录" />
        </view>
      </view>

    </view>
  </s-layout>
</template>

<script setup>
  import empty from '@/static/data-empty.png'
  import detailNavbar from './components/detail-navbar.vue';
  import ClassApi from '@/sheep/api/member/class';
  import sheep from '@/sheep';
  import {
    onLoad,
    onPageScroll,
    onPullDownRefresh
  } from '@dcloudio/uni-app';
  import {
    computed,
    ref,
    reactive
  } from 'vue';
  let weekList = ref([])
  let classDetail = reactive({})
  let current = ref(2)

  const statusBarHeight = sheep.$platform.device.statusBarHeight * 2;
  // 根据状态返回对应的 class
  const getStatusInfo = (status) => {
    const statusMap = {
      0: {
        class: 'status-waiting',
        text: '正常'
      },
      1: {
        class: 'status-enrolling',
        text: '停课'
      },
      2: {
        class: 'status-ended',
        text: '结班'
      }
    }
    // 如果状态未定义，返回默认值
    return statusMap[status] || {
      class: '',
      text: '未知'
    }
  }

  //显示地图
  const showMaoLocation = (e) => {
    // #ifdef H5
    const jsWxSdk = sheep.$platform.useProvider('wechat').jsWxSdk;
    if (jsWxSdk.isWechat()) {
      jsWxSdk.openLocation({
        latitude: Number(e.latitude),
        longitude: Number(e.longitude),
        name: e.name,
        address: `${e.detailAddress}`,
      });
    } else {
      // #endif
      uni.openLocation({
        latitude: Number(e.latitude),
        longitude: Number(e.longitude),
        name: e.name,
        address: `${e.detailAddress}`,
        success: function() {
          console.log('success');
        },
      });
      // #ifdef H5
    }
    // #endif
  };

  // 消课状态
  const getXkStatusInfo = (status) => {
    const statusMap = {
      0: {
        class: '',
        text: '待上课'
      },
      1: {
        class: 'status-end',
        text: '消课'
      },
      2: {
        class: 'status-error',
        text: '请假'
      },
      3: {
        class: 'status-warning',
        text: '旷课'
      }
    }
    return statusMap[status]
  }

  //获取当前周日期
  const getCurrentWeek = () => {
    let list = []
    const today = new Date()
    const currentDayOfWeek = today.getDay() // 0（周日）- 6（周六）
    const startOfWeek = new Date(today) // 从今天开始计算
    startOfWeek.setDate(today.getDate() - currentDayOfWeek + 1) // 周一

    for (let i = 0; i < 7; i++) {
      const date = new Date(startOfWeek)
      date.setDate(startOfWeek.getDate() + i)
      const dayOfWeek = ['一', '二', '三', '四', '五', '六', '日'][i]

      // 格式化为 MM/DD
      const formattedDate =
        `${String(date.getMonth() + 1).padStart(2, '0')}/${String(date.getDate()).padStart(2, '0')}`

      list.push({
        date: formattedDate, // 格式化为 MM/DD
        day: dayOfWeek,
        i: i + 1
      });
    }
    return list
  }

  // 格式化后台返回时间
  const formartDate = (dateString) => {
    if (dateString.length !== 8) {
      return dateString
    }
    // 截取字符串并添加分隔符
    const year = dateString.slice(0, 4);
    const month = dateString.slice(4, 6);
    const day = dateString.slice(6, 8);

    return `${year}-${month}-${day}`;
  }

  // 格式化后台返回周几
  const formartWeek = (dayNumber) => {
    const days = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周日']
    return days[dayNumber]
  }

  // 详情TAB
  const selTab = (index) => {
    current.value = index
  }
  const getClassDetail = (options) => {
    ClassApi.getClassInfo({
      id: options.id,
      studentId: options.studentId
    }).then(res => {
      if (res.code == 0) {
        res.data.startDate = formartDate(res.data.beginDate)
        res.data.endDate = formartDate(res.data.endDate)
        if (res.data?.courseRecordList.length) {
          res.data.courseRecordList.forEach(item => {
            item.dateTime = formartDate(item.dateTime)
          })
        }
        classDetail = Object.assign(classDetail, res.data)
      } else {
        uni.showToast({
          icon: 'none',
          title: res.msg,
        })
      }

    })
  }
  onLoad((options) => {
    getClassDetail(options)
    weekList.value = getCurrentWeek()
  })
</script>

<style lang="scss" scoped>
  .list-item {
    background: var(--ui-BG-1) !important;

    .img-box {
      font-size: 0;
      position: relative;
      height: 360rpx;
      width: 100%;

      image {
        width: 100%;
        height: 100%;
        object-fit: cover;
        // border-radius: 16rpx 16rpx 0 0;
      }

      .status {
        left: 0;

        &.status-waiting {}

        &.status-enrolling {}

        &.status-ended {}
      }

      .tag {
        right: 0;
      }

      .tag,
      .status {
        position: absolute;
        bottom: 0;
        padding: 10rpx 20rpx;
        background-color: rgba(0, 0, 0, 0.6);
        color: #fff;
        font-size: 28rpx;
      }
    }

    .item-card {
      background: #fff;
      border-radius: 10rpx;
      margin: 14rpx 20rpx;

      .tab-list {
        .tab-item {
          width: 50%;
          text-align: center;
          line-height: 48rpx;
          font-size: 30rpx;
          font-weight: bold;
          padding: 0 24rpx 10rpx;

          &.act {
            color: #FF6000;
            position: relative;
            font-weight: bold;

            &:after {
              height: 4rpx;
              width: 48rpx;
              position: absolute;
              left: 50%;
              transform: translateX(-50%);
              content: " ";
              background-color: #FF6000;
              bottom: 10rpx;
            }
          }
        }
      }

      .tab-detail {
        &.use-history {
          padding: 0 20rpx;

          .use-list-item {
            >view {
              width: 33.33%;
            }

            line-height: 68rpx;
            border-bottom: 1px solid #eee;

            .day {
              text-align: center;
            }

            &:last-of-type {
              border: none;
            }

            .sts {
              text-align: right;

              &.status-error {
                color: #e6a23c;
              }

              &.status-end {
                color: #409eff;
              }

              &.status-warning {
                color: #f56c6c;
              }
            }
          }
        }
      }
    }

    .time-tab {
      text-align: center;

      .txt {
        width: 14%;
        border-right: 1px solid #e9e9e9;
        letter-spacing: 8rpx;
      }

      .tab-box {
        padding-left: 10rpx;

        .group {
          font-size: 20rpx;

          .date {
            height: 40rpx;
            line-height: 40rpx;
          }

          .rad {
            width: 40rpx;
          }

          .act {
            background: #FF6000;
            color: #fff;
            border-radius: 50%;
          }
        }
      }

    }

    .header-avatar {
      width: 100rpx;
      height: 100rpx;
      background: #ececec;
      border-radius: 50%;
      border: 4rpx solid #edc36c;
      position: relative;
      box-sizing: border-box;
      margin-bottom: 20rpx;

      .avatar-img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    .btn {
      background: linear-gradient(to right, #FF6000, #FE832A);
      height: 50rpx;
      line-height: 50rpx;
      padding: 0 20rpx;
      border-radius: 25rpx;
      font-size: 24rpx;
      color: #fff;
    }

    .title-text {
      font-size: 30rpx;
      font-weight: bold;
      line-height: 42rpx;
    }

    .subtitle-text {
      font-size: 26rpx;
      font-weight: 400;
      color: $dark-9;
      line-height: 42rpx;
    }
  }
</style>